#include "StdAfx.h"
#include "SocketManage.h"
#include "stationDlg.h"
#include "SocketThread.h"

CSocketManage::CSocketManage(CstationDlg * p)
:pdlg( p )
,pSocketThread( NULL )
{
	
}

CSocketManage::~CSocketManage(void)
{
	//TerminateThread( pSocketThread->m_hThread,0 ) ;
	::PostThreadMessage( pSocketThread->m_nThreadID,WM_QUIT,0,0);
	if ( ::WaitForSingleObject( pSocketThread->m_hThread,100 ) == WAIT_TIMEOUT )
	{
		TRACE("TerminateThread ::.....\r\n");
		::TerminateThread( pSocketThread->m_hThread,1 ) ;
	}
}

int CSocketManage::startMonitor(void)
{
	//::AfxBeginThread( (AFX_THREADPROC)_ThreadstartMonitor,(LPVOID) this );
	pSocketThread = ::AfxBeginThread(RUNTIME_CLASS( CSocketThread));
	return 0;
}

//int CSocketManage::_ThreadstartMonitor(LPVOID pParam)
//{
//	CSocketManage * pthis = static_cast< CSocketManage * > ( pParam ) ;
//	if ( pthis )
//	{
//		pthis->_startMonitor();
//	}
//	
//	return 0;
//}
//int CSocketManage::_startMonitor(void )
//{
//	
//	while(!socketVector.empty())
//	{
//		
//	}
//	return 0;
//}
bool CSocketManage::addStationToManager(station& sta)
{
	static_cast< CSocketThread*>(pSocketThread)->addSocketToThread(sta);
	return true;
}

void CSocketManage::deleteStationSocket(const CString & staid)
{
	static_cast< CSocketThread*>(pSocketThread)->deleteStationSocket(staid);
}
